package xyz.hzxiao.result;

import org.springframework.http.HttpStatus;
import xyz.hzxiao.base.AbstractResult;
import xyz.hzxiao.base.IMessage;

/**
 * @Author: scy
 * @Date: 2022-07-28 17:33
 */
public class JsonResult<T> extends AbstractResult<T> {

    private static final long serialVersionUID = 2372169689834212971L;

    public static <T> JsonResultBuilder<T> builder() {
        return new JsonResultBuilder<>();
    }

    public static <T> JsonResult<T> ok() {
        return JsonResult.<T>builder()
                .setSuccess(Boolean.TRUE)
                .setCode(HttpStatus.OK.value())
                .setMessage("操作成功")
                .setTotal(0L)
                .setPage(0L)
                .build();
    }

    public static <T> JsonResult<T> ok(T data) {
        return JsonResult.<T>builder()
                .setSuccess(Boolean.TRUE)
                .setCode(HttpStatus.OK.value())
                .setMessage("操作成功")
                .setTotal(0L)
                .setPage(0L)
                .setData(data)
                .build();
    }

    public static <T> JsonResult<T> ok(T data, Long total, Long page) {
        return JsonResult.<T>builder()
                .setSuccess(Boolean.TRUE)
                .setCode(HttpStatus.OK.value())
                .setMessage("操作成功")
                .setTotal(total)
                .setPage(page)
                .setData(data)
                .build();
    }

    public static <T> JsonResult<T> fail(IMessage message) {
        return JsonResult.<T>builder()
                .setSuccess(Boolean.FALSE)
                .setCode(message.getCode())
                .setMessage(message.getMessage())
                .build();
    }

    public static <T> JsonResult<T> fail(String message) {
        return JsonResult.<T>builder()
                .setSuccess(Boolean.FALSE)
                .setCode(HttpStatus.BAD_REQUEST.value())
                .setMessage(message)
                .build();
    }

    public static <T> JsonResult<T> fail(Integer code, String message) {
        return JsonResult.<T>builder()
                .setSuccess(Boolean.FALSE)
                .setCode(code)
                .setMessage(message)
                .build();
    }

    public static class JsonResultBuilder<T> extends AbstractResult<T> {

        private static final long serialVersionUID = -857846231599453015L;

        public JsonResult<T> build() {
            JsonResult<T> result = new JsonResult<>();
            result.success = this.success;
            result.code = this.code;
            result.message = this.message;
            result.total = this.total;
            result.page = this.page;
            result.data = this.data;
            return result;
        }

        public JsonResultBuilder<T> setSuccess(Boolean success) {
            this.success = success;
            return this;
        }

        public JsonResultBuilder<T> setCode(Integer code) {
            this.code = code;
            return this;
        }

        public JsonResultBuilder<T> setMessage(String message) {
            this.message = message;
            return this;
        }

        public JsonResultBuilder<T> setTotal(Long total) {
            this.total = total;
            return this;
        }

        public JsonResultBuilder<T> setPage(Long page) {
            this.page = page;
            return this;
        }

        public JsonResultBuilder<T> setData(T data) {
            this.data = data;
            return this;
        }
    }

}
